package com.my.main.controller.device;


/**
 * 设备管理
 *
 * @author ysy 2021-06-08
 */


import com.my.main.base.controller.BaseController;
import com.my.main.base.entity.Result;
import com.my.main.base.entity.ResultCode;
import com.my.main.common.utils.CurrencyUtil;
import com.my.main.common.utils.DateUtil;
import com.my.main.entity.device.Device;
import com.my.main.entity.enterprise.Enterprise;
import com.my.main.service.device.DeviceService;
import com.my.main.service.enterprise.EnterpriseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//1.解决跨域
@CrossOrigin
//2.声明restContoller
@RestController
//3.设置父路径
@RequestMapping(value="/api/v1/sys/device")
public class DeviceController extends BaseController {


    @Autowired
    private DeviceService deviceService;
    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 查询 - 设备管理--设备详情查询
     *
     * @param map
     *
     * @author ysy 2021-06-08
     */
    @ApiOperation(value="设备管理 - 设备详情查询")
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    public Result queryData(@RequestBody Map<String, Object> map) {
        Map<String, Object> resMap = new HashMap<>();
        if (StringUtils.isEmpty(map.get("deviceId"))) {
            return new Result(ResultCode.FAIL);
        }

        try {
            Device device = deviceService.queryStringId(map.get("deviceId").toString());
            resMap.put("deviceId", device.getDeviceId());
            resMap.put("name", device.getName());
            resMap.put("code", device.getCode());
            resMap.put("contacts", device.getContacts() == null ? "" : device.getContacts());
            resMap.put("contactsPhone", device.getContactsPhone() == null ? "" : device.getContactsPhone());
            resMap.put("position", device.getPosition() == null ? "" : device.getPosition());
            resMap.put("addTime", device.getAddTime().getTime());
            if (!StringUtils.isEmpty(device.getEnterpriseId())) {
                resMap.put("enterpriseId", device.getEnterpriseId());
                Enterprise enterprise = enterpriseService.queryStringId(device.getEnterpriseId());
                resMap.put("enterprise", enterprise == null ? "查询错误" : enterprise.getName());
                switch (device.getStatus()) {
                    case 0:
                        resMap.put("status", "离线");
                        break;
                    case 1:
                        resMap.put("status", "在线");
                        break;
                    case 2:
                        resMap.put("status", "故障");
                        break;
                }
            } else {
                resMap.put("enterpriseId", "");
                resMap.put("enterprise", "");
                resMap.put("status", "闲置");
            }
            return new Result(ResultCode.SUCCESS, resMap);
        } catch (Exception e) {
            logger.error(e);
            return new Result(ResultCode.FAIL, "系统错误，请联系管理员");
        }
    }


    /**
     * 查询 - 设备管理--设备列表查询
     *
     * @param map(page: 页数, limit: 每页条数)
     *
     * @author ysy 2021-06-08
     */
    @ApiOperation(value="设备管理 - 设备列表查询")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    public Result queryList(@RequestBody Map<String, Object> map) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("page", map.get("page"));
        resMap.put("limit", map.get("limit"));

        try {
            List<Map<String, Object>> deviceList = new ArrayList<>();

            map.put("page", CurrencyUtil.PageRowNum(Integer.parseInt(map.get("page").toString()), Integer.parseInt(map.get("limit").toString())));
            if (!userInfo.getIsAdmin()) {
                map.put("enterpriseList", userInfo.getEnterpriseList());
            }
            List<Device> devices = deviceService.queryList(map);
            for(Device device: devices) {
                Map<String, Object> deviceMap = new HashMap<>();
                deviceMap.put("deviceId", device.getDeviceId());
                deviceMap.put("name", device.getName());
                deviceMap.put("code", device.getCode());
                deviceMap.put("contacts", device.getContacts() == null ? "" : device.getContacts());
                deviceMap.put("contactsPhone", device.getContactsPhone() == null ? "" : device.getContactsPhone());
                deviceMap.put("position", device.getPosition() == null ? "" : device.getPosition());
                deviceMap.put("addTime", device.getAddTime().getTime());
                if (!StringUtils.isEmpty(device.getEnterpriseId())) {
                    deviceMap.put("enterpriseId", device.getEnterpriseId());
                    Enterprise enterprise = enterpriseService.queryStringId(device.getEnterpriseId());
                    deviceMap.put("enterprise", enterprise == null ? "查询错误" : enterprise.getName());
                    switch (device.getStatus()) {
                        case 0:
                            deviceMap.put("status", "离线");
                            break;
                        case 1:
                            deviceMap.put("status", "在线");
                            break;
                        case 2:
                            deviceMap.put("status", "故障");
                            break;
                    }
                } else {
                    deviceMap.put("enterpriseId", "");
                    deviceMap.put("enterprise", "");
                    deviceMap.put("status", "闲置");
                }
                deviceList.add(deviceMap);
            }
            resMap.put("deviceList", deviceList);
            resMap.put("totalCount", deviceService.queryCount(map));
            return new Result(ResultCode.SUCCESS, resMap);
        } catch (Exception e) {
            logger.error(e);
            return new Result(ResultCode.FAIL, "系统错误，请联系管理员");
        }
    }


    /**
     * 保存 - 设备管理--设备保存
     *
     * @param map
     *
     * @author ysy 2021-06-08
     */
    @ApiOperation(value="设备管理 - 设备保存")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody Map<String, Object> map) {
        if (StringUtils.isEmpty(map.get("name"))) {
            return new Result(ResultCode.FAIL, "必填项未填写");
        }
        if (StringUtils.isEmpty(map.get("code"))) {
            return new Result(ResultCode.FAIL, "必填项未填写");
        }

        try {
            // 新增设备管理--设备
            if (StringUtils.isEmpty(map.get("deviceId"))) {
                Device device = new Device();
                String id = idWorker.nextId() + "";
                device.setDeviceId(id);
                device.setName(map.get("name").toString());
                device.setCode("SB" + DateUtil.getFormatDate("-yyyyMMddHHmmss-") + id.substring(15));
                device.setEnterpriseId(StringUtils.isEmpty(map.get("enterpriseId")) ? null : map.get("enterpriseId").toString());
                device.setContacts(StringUtils.isEmpty(map.get("contacts")) ? null : map.get("contacts").toString());
                device.setContactsPhone(StringUtils.isEmpty(map.get("contactsPhone")) ? null : map.get("contactsPhone").toString());
                device.setPassword(StringUtils.isEmpty(map.get("password")) ? null : map.get("password").toString());
                device.setLive(StringUtils.isEmpty(map.get("live")) ? null : map.get("live").toString());
                device.setReplay(StringUtils.isEmpty(map.get("replay")) ? null : map.get("replay").toString());
                device.setStatus(0);
                device.setPosition(StringUtils.isEmpty(map.get("position")) ? null : map.get("position").toString());
                device.setAddTime(new Date());
                device.setIsDel(false);
                deviceService.isSave(device);
            } else {
                Device device = deviceService.queryStringId(map.get("deviceId").toString());
                if (device == null) {
                    return new Result(ResultCode.FAIL, "未找到设备");
                }
                device.setName(map.get("name").toString());
                device.setEnterpriseId(StringUtils.isEmpty(map.get("enterpriseId")) ? null : map.get("enterpriseId").toString());
                device.setContacts(StringUtils.isEmpty(map.get("contacts")) ? null : map.get("contacts").toString());
                device.setContactsPhone(StringUtils.isEmpty(map.get("contactsPhone")) ? null : map.get("contactsPhone").toString());
                device.setPassword(StringUtils.isEmpty(map.get("password")) ? null : map.get("password").toString());
                device.setLive(StringUtils.isEmpty(map.get("live")) ? null : map.get("live").toString());
                device.setReplay(StringUtils.isEmpty(map.get("replay")) ? null : map.get("replay").toString());
                device.setPosition(StringUtils.isEmpty(map.get("position")) ? null : map.get("position").toString());
                deviceService.isUpdate(device);
            }

            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            logger.error(e);
            return new Result(ResultCode.FAIL,"系统错误，请联系管理员");
        }
    }


    /**
     * 删除 - 设备管理--设备删除
     *
     * @param map
     *
     * @author ysy 2021-06-08
     */
    @ApiOperation(value="设备管理 - 设备删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@RequestBody Map<String, Object> map) {
        if (StringUtils.isEmpty(map.get("deviceId"))) {
            return new Result(ResultCode.FAIL);
        }

        try {
            deviceService.isDeleteByUuid(map.get("deviceId").toString());
            return new Result(ResultCode.SUCCESS);
        } catch (Exception e) {
            logger.error(e);
            return new Result(ResultCode.FAIL,"系统错误，请联系管理员");
        }

    }


}
